<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie';
    import qs from 'qs';
    import util from '../libs/util';
    export default {
        data () {
            const validateLoginCheck = (rule, value, callback) => {
                if (this.form.login_flag) {
                    callback(new Error('账号或密码错误!'));
                } else if (value === '') {
                    if (rule.field === 'userName') {
                        callback(new Error('账号不能为空'));
                    } else {
                        callback(new Error('密码不能为空'));
                    }
                } else {
                    callback();
                }
            };
            return {
                form: {
                    userName: '',
                    password: '',
                    login_flag: false
                },
                rules: {
                    userName: [
                        { validator: validateLoginCheck, trigger: 'blur' }
                    ],
                    password: [
                        { validator: validateLoginCheck, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            handleSubmit () {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        let params = {
                            'username': this.form.userName,
                            'password': this.form.password
                        };
                        util.ajax.post('api/login', qs.stringify(params)).then(res => {
                            if (res.data.code === 200) {
                                // let token = res.data.data.token;
                                // 登录成功
                                Cookies.set('user', this.form.userName);
                                Cookies.set('access', 0);

                                let avatorImgPath = res.data.data.set_avator;
                                if (avatorImgPath.indexOf('http') === -1) {
                                    console.log('avatorImgPath -->', avatorImgPath);
                                    avatorImgPath = util.ajaxUrl + '/' + avatorImgPath;
                                }
                                // let setAvator = util.ajaxUrl + '/' + res.data.data.set_avator;
                                this.$store.commit('setAvator', avatorImgPath);
                                this.$store.commit('initUserInfo', res.data.data);
                                util.ajax.get('api/role/get_rules/' + res.data.data.role_id).then(res => {
                                    if (res.data.code === 200) {
                                        this.$store.commit('updateRulesList', res.data.data.rules_list);
                                        this.$store.commit('updateMenulist');
                                    }
                                });

                                // this.$store.commit('token', token);
                                // if (res.data.data.head_portrait) {
                                //     this.$store.commit('setAvator', res.data.data.head_portrait);
                                // } else {
                                //     this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
                                // }

                                this.$router.push({
                                    name: 'home_index'
                                });
                            } else {
                                // 登录失败
                                this.form.login_flag = true;
                                this.$refs.loginForm.validateField('userName');
                                this.$refs.loginForm.validateField('password');
                                this.form.login_flag = false;
                            }
                        });
                    }
                });
            }
        }
    };
</script>

<style>

</style>
